package bean;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import luceneutil.V100LunceneWorker;
import applicationutil.ApplicationConstant;
import applicationutil.ApplicationUtil;
import dao.V100Dao;
import entities.V100;

public class IndexControl {
	private ScheduledFuture<?> beeperHandle;
	private static StringBuilder LOG = new StringBuilder("");

	public static synchronized StringBuilder getLog() {
		return LOG;
	}

	public IndexControl() {
		// TODO Auto-generated constructor stub
	}

	private final ScheduledExecutorService scheduler = Executors
			.newScheduledThreadPool(1);

	private void toRun() {
		System.out.println("----torun running");
		V100LunceneWorker lunceneWorker = new V100LunceneWorker(
				ApplicationConstant.INDEX_PATH);
		lunceneWorker.createIndex();
		lunceneWorker.createIndexWriter();
		/*
		 * lay list can cap nhat, download noi dung va cap nhat lay vao db
		 */
		V100Dao v100Dao = (V100Dao) ApplicationUtil.getService("v100DAO");
		List<V100> list = v100Dao.getListV100Update(0, 5);
		for (V100 v : list) {
			System.out.println("---id:" + v.getPv100());
			ApplicationUtil.addToLog("---updating url with pv100 ="
					+ v.getPv100());
			ApplicationUtil.getInforFromURL(v);
			if (v.getVv102() == null)
				v.setVv102("");
			/*
			 * neu khi cap nhat noi dung rong, thi xoa bo document nay trong chi
			 * muc
			 */
			if (v.getVv103() == null) {
				v.setVv103("");
			}
			v100Dao.insertV100(v.getPv100(), v.getVv101(), v.getVv102(),
					v.getVv103());
			/*
			 * insert sub url, max 50 url per one page
			 */
			if (v.getSubUrls().size() < 50) {
				for (String subUrl : v.getSubUrls()) {
					v100Dao.insertV100(0, subUrl, null, null);
				}
			} else {
				for(int i=0;i<49;i++)
					v100Dao.insertV100(0, v.getSubUrls().get(i), null, null);
			}
		}
		/*
		 * index
		 */
		List<V100> listIndex = v100Dao.getListV100Index(0, 10);
		for (V100 v : listIndex) {
			ApplicationUtil.addToLog("---indexing url:" + v.getVv101());
			System.out.println("---id indx:" + v.getPv100());
			lunceneWorker.update(v);
		}
		Date date = new Date();
		DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
		ApplicationUtil
				.addToLog("---------------------------------------END 1 TASK AT "
						+ dateFormat.format(date)
						+ "-----------------------------------------------");
		System.out
				.println("---------------------------------------END 1 TASK AT "
						+ dateFormat.format(date)
						+ "-----------------------------------------------");
		try {
			lunceneWorker.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void run() {
		final Runnable beeper = new Runnable() {
			public void run() {
				// SET CO, neu admin tat v100stop=0 thi dung danh chi muc
				V100Dao v100Dao = (V100Dao) ApplicationUtil
						.getService("v100DAO");
				if (v100Dao.getRoleIndexing() == 0) {
					// beeperHandle.cancel(true);
					Date date = new Date();
					DateFormat dateFormat = new SimpleDateFormat(
							"dd-MM-yyyy hh:mm:ss");
					ApplicationUtil
							.addToLog("---------------------------------------STOP AUTO INDEXING AT "
									+ dateFormat.format(date)
									+ "-----------------------------------------------");
					System.out
							.println("---------------------------------------STOP AUTO INDEXING AT "
									+ dateFormat.format(date)
									+ "-----------------------------------------------");
				} else
					toRun();
			}
		};
		beeperHandle = scheduler.scheduleAtFixedRate(beeper, 0, 30,
				TimeUnit.SECONDS);
		/*
		 * if(beeperHandle.isCancelled()||beeperHandle.isDone()){ beeperHandle =
		 * scheduler.scheduleAtFixedRate(beeper, 0, 30, TimeUnit.SECONDS); }
		 */

	}
}
